package com.xiaoxu.spzx.manager.controller;

import com.github.pagehelper.PageInfo;
import com.xiaoxu.spzx.log.annotation.Log;
import com.xiaoxu.spzx.manager.service.SysRoleService;
import com.xiaoxu.spzx.model.dto.system.SysRoleDto;
import com.xiaoxu.spzx.model.entity.system.SysRole;
import com.xiaoxu.spzx.model.vo.common.Result;
import com.xiaoxu.spzx.model.vo.common.ResultCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping(value = "/admin/system/sysRole")
public class SysRoleController {
    @Autowired
    private SysRoleService sysRoleService;



    //分配角色 - > 1.查询所有的角色
    @GetMapping(value = "/findAllRoles/{userId}")
    public Result findAllRoles(@PathVariable("userId") Long userId){
       Map<String,Object> map  =  sysRoleService.findAll(userId);
        return Result.build(map,ResultCodeEnum.SUCCESS);

    }

    //current 当前页 limit 每页记录条数

    @PostMapping("/findByPage/{current}/{limit}")
    public Result findByPage(@PathVariable(value = "current") Integer current ,
                             @PathVariable(value = "limit") Integer limit,
                             @RequestBody SysRoleDto sysRoleDto) {
        //pageHelper实现分页
        PageInfo<SysRole> pageInfo = sysRoleService.findByPage(sysRoleDto,current,limit) ;
        return Result.build(pageInfo , ResultCodeEnum.SUCCESS) ;
    }
    @Log(title = "角色添加",businessType = 1)
    // 添加角色的handler
    @PostMapping("/saveSysRole")
    public Result saveSysRole(@RequestBody SysRole sysRole){

       sysRoleService.saveSysRole(sysRole);

       return Result.build(null,ResultCodeEnum.SUCCESS);

    }


    //角色修改的接口

    @PutMapping("/updateSysRole")
    public Result updateSysRole(@RequestBody SysRole sysRole){

        sysRoleService.updateSysRole(sysRole);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }



    //角色逻辑删除的handler
    @DeleteMapping("/deleteById/{roleId}")
    public Result deleteById(@PathVariable("roleId") Long roleId){
        sysRoleService.deleteById(roleId);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }





}
